package com.zhenhao.xsq.utils;

/**
 * Created by William on 2020/8/14.
 */
public class ScaleUtil {
    public ScaleUtil() {
    }

    public static int scaleChage(String s) {
        Long value = Long.parseLong(s, 16);
        return value.intValue();
    }

    public static String str2HexStr(String str) {
        char[] chars = "fda50693a4e24fb1afcfc6eb07647825".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();

        for(int i = 0; i < bs.length; ++i) {
            int bit = (bs[i] & 240) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 15;
            sb.append(chars[bit]);
        }

        return sb.toString().trim();
    }

    public static void sendSerialPort(String data) {
        if (!data.isEmpty()) {
            data = data.toUpperCase();
            int len = data.length() / 2;
            byte[] sendData = new byte[len];
            char[] hc = data.toCharArray();

            for(int i = 0; i < len; ++i) {
                int p = 2 * i;
                sendData[i] = (byte)(charToByte(hc[p]) << 4 | charToByte(hc[p + 1]));
                System.out.println(sendData[i]);
            }
        }

    }

    public static byte charToByte(char c) {
        return (byte)c;
    }

    public static byte[] StringToByteArray(String str) {
        String[] str_ary = str.split("");
        int n = str_ary.length;
        byte[] bt_ary = new byte[n];

        for(int i = 0; i < n; ++i) {
            bt_ary[i] = (byte) Integer.parseInt(str_ary[i], 16);
        }

        return bt_ary;
    }

    public String ByteArrayToString(byte[] bt_ary) {
        StringBuilder sb = new StringBuilder();
        if (bt_ary != null) {
            byte[] var3 = bt_ary;
            int var4 = bt_ary.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                byte b = var3[var5];
                sb.append(String.format("%02X ", b));
            }
        }

        return sb.toString();
    }

    public String ByteToString(byte bt) {
        return String.format("%08d", Integer.parseInt(Integer.toBinaryString(bt)));
    }

    public static byte hexTobyte(String hex) {
        return (byte) Integer.parseInt(hex, 16);
    }

    public static byte[] hexTobytes(String hex) {
        if (hex.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hex.length() / 2];
            int j = 0;

            for(int i = 0; i < hex.length(); i += 2) {
                result[j++] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
            }

            return result;
        }
    }

    public static void main(String[] args) {
        byte[] x = new byte[2];
        int s = 255;
        byte c = (byte)scaleChage(s + "");
        System.out.println(c);
    }

    public static int asc2Hex(int data) {
        int li;
        if (data >= 48 && data <= 57) {
            li = data - 48;
        } else if (data >= 65 && data <= 90) {
            li = data - 55;
        } else if (data >= 97 && data <= 122) {
            li = data - 87;
        } else {
            li = data;
        }

        return li;
    }

    public static String byte2String(byte[] data) {
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[data.length * 2];

        for(int j = 0; j < data.length; ++j) {
            int v = data[j] & 255;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 15];
        }

        String result = new String(hexChars);
        result = result.replace(" ", "");
        return result;
    }
}
